<template>
	<uv-popup ref="popupBox" mode="bottom" :round="10" @maskClick="closeFunc">
		<view class="title">提交勘场报告</view>
		<scroll-view class="scroll-view" scroll-y>
			<view class="uv-form-box">
				<uv-form labelPosition="top" :model="formData" :rules="rules" ref="form" labelWidth="auto">
					<uv-form-item label="现场照片 (.jpg,.png,.gif)" prop="scene_file" labelPosition="left" required>
						<view class="file-box">
							<uv-button type="primary" text="点击上传" @click="uploadFunc('scene_file')"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-media" v-for="(item, index) in formData.scene_file" :key="index">
							<view class="del-icon" @click.stop="delFile(index,'scene_file')">
								<uv-icon name="trash-fill" size="18" color="#ffffff"></uv-icon>
							</view>
							<image mode="aspectFill" :src="item.tempFilePath" class="media-item"></image>
						</view>
					</view>
					<uv-form-item label="勘场报告 (.jpg,.png,.gif,.xlsx,.xls)" prop="report_file" labelPosition="left" required>
						<view class="file-box">
							<uv-button type="primary" text="点击上传" @click="uploadFunc('report_file')"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-media" v-for="(item, index) in formData.report_file" :key="index">
							<view class="del-icon" @click.stop="delFile(index,'report_file')">
								<uv-icon name="trash-fill" size="18" color="#ffffff"></uv-icon>
							</view>
							<image mode="aspectFill" :src="item.tempFilePath" class="media-item"></image>
						</view>
					</view>
					<uv-form-item label="视频 (.mp4,.3GP,.WMV)" prop="video_file" labelPosition="left" required>
						<view class="file-box">
							<uv-button type="primary" text="点击上传" @click="uploadFunc('video_file')"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-media" v-for="(item, index) in formData.video_file" :key="index">
							<view class="icon-box">
								<uv-icon name="play-right-fill" size="20" color="#ffffff"></uv-icon>
							</view>
							<view class="del-icon" @click.stop="delFile(index,'video_file')">
								<uv-icon name="trash-fill" size="18" color="#ffffff"></uv-icon>
							</view>
							<image mode="aspectFill" :src="item.thumbTempFilePath" class="media-item"></image>
						</view>
					</view>
					<uv-form-item label="手绘图 (.jpg,.png,.gif)" prop="sketch_file" labelPosition="left" required>
						<view class="file-box">
							<uv-button type="primary" text="点击上传" @click="uploadFunc('sketch_file')"
								customTextStyle="font-weight: 400;font-size: 24rpx;color: #FFFFFF;"
								customStyle="width: 170rpx;height: 60rpx;background: #3B75FF;border-radius: 16rpx;"></uv-button>
						</view>
					</uv-form-item>
					<view class="file-list">
						<view class="file-media" v-for="(item, index) in formData.sketch_file" :key="index">
							<view class="del-icon" @click.stop="delFile(index,'sketch_file')">
								<uv-icon name="trash-fill" size="18" color="#ffffff"></uv-icon>
							</view>
							<image mode="aspectFill" :src="item.tempFilePath" class="media-item"></image>
						</view>
					</view>
					<uv-form-item label="备注" prop="remark">
						<uv-textarea height="100" v-model="formData.remark" placeholder="请输入内容"></uv-textarea>
					</uv-form-item>
				</uv-form>
			</view>
		</scroll-view>
		<view class="btn-box">
			<uv-button @click="closeFunc" text="取 消" customTextStyle="font-weight: 400;font-size: 28rpx;color: #8F8F8F;"
				customStyle="width: 180rpx;height: 80rpx;background: #FFFFFF;border-radius: 16rpx;border: 2rpx solid #E3E3E3;margin-bottom:20rpx;"></uv-button>
			<uv-button @click="formSubmit" type="primary" text="提 交"
				customTextStyle="font-weight: 400;font-size: 28rpx;color: #FFFFFF;"
				customStyle="width: 500rpx;height: 80rpx;background: #3B75FF;border-radius: 16rpx;margin-bottom:20rpx;"></uv-button>
		</view>
	</uv-popup>
</template>

<script setup>
	import {
		computed,
		watch,
		onMounted,
		getCurrentInstance,
		ref,
		defineEmits
	} from 'vue';
	const {
		proxy: that
	} = getCurrentInstance();
	let props = defineProps({
		show: {
			type: Boolean,
			default: () => {
				return false;
			}
		},
		dataObj: {
			type: Object,
			default: () => {
				return {};
			}
		}
	});
	// 表单数据
	let formData = ref({
		scene_file: [],
		report_file: [],
		video_file: [],
		sketch_file: [],
		remark: ''
	});
	// 表单规则
	let rules = ref({
		scene_file: [{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传现场照片'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
		}],
		report_file: [{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传勘察报告'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
		}],
		video_file: [{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传视频'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
		}],
		sketch_file: [{
			asyncValidator: (rule, value, callback) => {
				if (!value.length) {
					callback(new Error('请上传手绘图'));
				} else {
					// 如果校验通过，也要执行callback()回调
					callback();
				}
			}
		}]
	});

	onMounted(() => {
		// that.$refs.popupBox.open();
		// getCompanyList();
	});

	watch(
		() => props.show,
		(newValue, oldValue) => {
			console.log(newValue, oldValue, '值变化');
			if (props.show) {
				that.$refs.popupBox.open();
			} else {
				formData.value = {
					scene_file: [],
					report_file: [],
					video_file: [],
					sketch_file: [],
					remark: ''
				};
				that.$refs.form.clearValidate();
				that.$refs.popupBox.close();
			}
		}
	);
	const emit = defineEmits(['close', 'onSuccess']);

	const closeFunc = () => {
		emit('close', false);
	};
	// 上传文件
	const uploadFunc = (type) => {
		switch (type) {
			case 'scene_file':
				uni.chooseMedia({
					mediaType: ['image'], //, 'video'
					sourceType: ['album', 'camera'],
					// count: 1,
					success(res) {
						res.tempFiles.map((item) => {
							formData.value[type].push(item);
							that.$refs.form.validateField(type);
						});
					}
				});
				break;
			case 'report_file':
				uni.chooseMessageFile({
					// count: 9, //限制选择的文件数量
					type: 'file', //非图片和视频的文件,不选默认为all，file，video，image
					extension: ['jpg', 'png','gif','xlsx', 'xls'],//.jpg,.png,.gif,.xlsx,.xls
					success(res) {
						console.log(res.tempFiles);
						res.tempFiles.map((item) => {
							formData.value[type].push(item);
							that.$refs.form.validateField(type);
						});
					}
				});
				break;
			case 'video_file':
				uni.chooseMedia({
					mediaType: ['video'], //, 'video'
					sourceType: ['album', 'camera'],
					// count: 1,
					success(res) {
						console.log(res.tempFiles)
						res.tempFiles.map((item) => {
							formData.value[type].push(item);
							that.$refs.form.validateField(type);
						});
					}
				});
				break;
			case 'sketch_file':
				uni.chooseMedia({
					mediaType: ['image'], //, 'video'
					sourceType: ['album', 'camera'],
					// count: 1,
					success(res) {
						res.tempFiles.map((item) => {
							formData.value[type].push(item);
							that.$refs.form.validateField(type);
						});
					}
				});
				break;
			default:
		}
	};
	// 删除文件
	const delFile = (index, type) => {
		formData.value[type].splice(index, 1);
		that.$refs.form.validateField(type);
	};
	// 表单提交
	const formSubmit = () => {
		let data = formData.value;
		let dataObj = props.dataObj;
		console.log(data, '表单数据', dataObj);
		that.$refs.form
			.validate()
			.then((res) => {
				uni.showLoading({
					mask: true,
					title: '文件上传中...'
				});
				let file1 = uploadAjax(data.scene_file, 'scene_file');
				let file2 = uploadAjax(data.report_file, 'report_file');
				let file3 = uploadAjax(data.video_file, 'video_file');
				let file4 = uploadAjax(data.sketch_file, 'sketch_file');
				Promise.all([file1, file2, file3, file4]).then((result) => {
					uni.hideLoading()
					let obj = {
						remark: data.remark,
						project_id: props.dataObj.id
					};
					console.log(result) //['成功了', 'success']
					result.forEach(item => {
						obj[item.type] = item.res.arrResult;
					})
					console.log(obj, "参数")
					uni.showLoading({
						mask: true,
						title: '信息提交中...'
					});
					that.$Api
						.wxSubmitSurveyInformation(obj)
						.then((res) => {
							uni.hideLoading();
							if (res.code == 200) {
								emit('onSuccess', true);
							}
							uni.showToast({
								title: res.code == 200 ? '操作成功' : res.message,
								icon: res.code == 200 ? 'success' : 'error'
							});
						})
						.catch((err) => {
							uni.hideLoading();
						});
				}).catch((error) => {
					console.log(error)
				})
			})
			.catch((errors) => {});
	};
	const uploadAjax = (fileList, type) => {
		return new Promise((resolve, reject) => {
			that.$upload(fileList).then(res => {
				console.log(res, "文件上传")
				resolve({
					type,
					res
				})
			})
		})
	}
</script>

<style lang="scss" scoped>
	.title {
		font-size: 32rpx;
		width: 100%;
		line-height: 100rpx;
		text-align: center;
	}

	.btn-box {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		box-sizing: border-box;
		margin-top: 30rpx;
	}

	.scroll-view {
		width: 100%;
		max-height: 50vh;
		background-color: #fff;

		// padding: 0 30rpx;
		// box-sizing: border-box;
		.uv-form-box {
			padding: 0 30rpx !important;
		}

		.file-box {
			width: 100%;
			display: flex;
			flex-direction: row-reverse;
		}

		.file-list {
			width: 100%;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			flex-wrap: wrap;

			.file-item {
				display: flex;
				justify-content: space-between;
				margin-top: 10rpx;
				margin-left: 10rpx;
				padding: 10rpx;
				background-color: #f8f8f8;

				.file-name {
					font-size: 24rpx;
				}
			}

			.file-media {
				width: 150rpx;
				height: 150rpx;
				margin: 0 27rpx 22rpx 0;
				overflow: hidden;
				border-radius: 10rpx;
				position: relative;

				&:nth-child(4n) {
					margin-right: 0;
				}

				.media-item {
					width: 100%;
					height: 100%;
				}

				.icon-box {
					position: absolute;
					z-index: 1;
					top: 50%;
					left: 50%;
					transform: translate(-50%, -50%);
					background-color: rgba(0, 0, 0, 0.6);
					width: 70rpx;
					height: 70rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 50%;
				}

				.del-icon {
					width: 60rpx;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: absolute;
					right: 10rpx;
					top: 10rpx;
					z-index: 1;
					background-color: rgba(0, 0, 0, 0.6);
					border-radius: 10rpx;
				}
			}
		}
	}
</style>